#include "utility.h"

int GetFreqencyListFromSampleFile(FILE *fp, int *list){
   if(list == NULL)
	   return 0;
   int * a = list;
   for(int i=0;i<95;i++)
   {
       a[i]=0;
   }
   int ch;
   ch=fgetc(fp);
   while(ch!=EOF)
   {
	   if(ch >= 0x20 && ch <= 0x7E)
		   a[ch-32]++;
       ch=fgetc(fp);
   }
   return 1;
}

int WriteFrequencyListToFile(FILE *fp, int *list){
	if(list == NULL)
		return 0;
    return fwrite(list,sizeof(int),95,fp) == 95;
}

int ReadFreqencyListFromFile(FILE *fp, int *list){
	if(list == NULL)
		return 0;
    int *n = list;
    return fread(n,sizeof(int),95,fp) == 95;
}

void ClearZeroFreqencySymbols(int *list, FILE *fp_in, FILE *fp_out){
	int c, i = 0;
	fseek(fp_in, 0, SEEK_SET);
	fseek(fp_out, 0, SEEK_SET);

	while((c=fgetc(fp_in)) != EOF){
		if((c >= 0x20 && c <= 0x7E && list[c - 0x20] > 0) || c == 0x0A || c== 0x0D)
			fputc(c, fp_out);
		else
			fprintf(stdout, "Symbol %c(%d) cleared at position %d.\n", c, c, i);
		i++;
	}
}
